package me.keiwu.pub.util;

import me.keiwu.pub.data.GlobalConst;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by kei on 06/11/2017.
 */
public class ContextUtil {

    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        if (null != cookies) {
            for (Cookie cookie : cookies) {
                if (name.equals(cookie.getName())) {
                    return cookie;
                }
            }
        }
        return null;
    }


    public static Long getLoginUserId(HttpServletRequest request) {
        Long userId = Long.valueOf(request.getHeader(GlobalConst.REQUEST_HEADER_USER_ID));
        if (userId == null) throw new NullPointerException("no userId set in request attribute!");
        return userId;
    }

    public static String getLoginUserNickName(HttpServletRequest request) {
        String nickName = String.valueOf(request.getHeader(GlobalConst.REQUEST_HEADER_NICK_NAME));
        if (nickName == null) throw new NullPointerException("no userId set in request attribute!");
        return nickName;
    }

}
